//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kingyea.esb.jmx;

import com.kingyea.esb.jmx.config.JMXServerConfig;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.*;
import javax.security.auth.Subject;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.util.*;
import java.util.regex.Pattern;
@Component
public final class Monitor {
    private static final Logger LOGGER = Logger.getLogger(Monitor.class);
    private MBeanServer server = JMXHelper.lookupMBeanServer();
    private JMXConnectorServer jmxConnectorServer;

    public Monitor() {
    }

    public void start() throws Exception {
        LOGGER.info("初始化JMX模块...");
        if (this.jmxConnectorServer != null && this.jmxConnectorServer.isActive()) {
            LOGGER.warn("已经初始化");
        } else if (JMXServerConfig.getConfig().getJmxServiceUrl() != null && !JMXServerConfig.getConfig().getJmxServiceUrl().isEmpty()) {
            if ("rmi".equals(JMXServerConfig.getConfig().getProtocal())) {
                if (JMXServerConfig.getConfig().getPort() == null) {
                    throw new JMXException("rmi协议需要配置端口");
                }

                LocateRegistry.createRegistry(JMXServerConfig.getConfig().getPort());
            }

            JMXServiceURL url = new JMXServiceURL(JMXServerConfig.getConfig().getJmxServiceUrl());
            Map environment = new HashMap();
            environment.put("jmx.remote.authenticator", new JMXAuthenticator() {
                public Subject authenticate(Object credentials) {
                    if (!(credentials instanceof String[])) {
                        throw new SecurityException("Bad credentials");
                    } else {
                        String[] creds = (String[])((String[])credentials);
                        if (creds.length != 2) {
                            throw new SecurityException("Bad credentials");
                        } else {
                            String user = creds[0];
                            String password = creds[1];
                            if (password != null && user != null) {
                                try {
                                    JMXServerConfig config = JMXServerConfig.getConfig();
                                    if (user.equals(config.getUserName()) && password.equals(config.getPassword())) {
                                        Set principals = new HashSet();
                                        principals.add(new JMXPrincipal(user));
                                        return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
                                    }
                                } catch (Exception var7) {
                                    var7.printStackTrace();
                                }

                                throw new SecurityException("Bad password");
                            } else {
                                throw new SecurityException("Bad password");
                            }
                        }
                    }
                }
            });
            JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, this.server);
            connectorServer.start();
            this.registerMBeans(this.server);
            LOGGER.info("JMX模块初始化完成");
        } else {
            throw new JMXException("需要配置JMX服务路径");
        }
    }

    public void registerMBean(String objectName, Object bean) {
        try {
            ObjectName name = ObjectName.getInstance(objectName);
            this.server.registerMBean(bean, name);
            LOGGER.info("注册MBean：" + objectName + "[" + objectName + "]");
        } catch (Exception var4) {
            LOGGER.warn("注册MBean失败：" + objectName, var4);
        }

    }

    public void unregisterMBean(String objectName) {
        try {
            ObjectName name = ObjectName.getInstance(objectName);
            this.server.unregisterMBean(name);
            LOGGER.info("卸载MBean：" + objectName + "[" + objectName + "]");
        } catch (Exception var3) {
            LOGGER.warn("卸载MBean失败：" + objectName, var3);
        }

    }

    private void registerMBeans(MBeanServer mbeanServer) {
        String[] beanNames = SpringContainer.getInstance().getBeanNames();
        String regex = "\\S+:\\S+=\\S+";
        Pattern pattern = Pattern.compile(regex);
        String[] arr$ = beanNames;
        int len$ = beanNames.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String name = arr$[i$];
            if (pattern.matcher(name).matches()) {
                try {
                    ObjectName objectName = ObjectName.getInstance(name);
                    mbeanServer.registerMBean(SpringContainer.getInstance().getBean(name), objectName);
                    LOGGER.info("注册MBean：" + name + "[" + name + "]");
                } catch (Exception var10) {
                    LOGGER.warn("注册MBean失败：" + name, var10);
                }
            }
        }

    }

    public void stop() throws IOException {
        if (this.jmxConnectorServer != null && this.jmxConnectorServer.isActive()) {
            this.jmxConnectorServer.stop();
        }

    }
}
